home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 3.iso / dist / fw_apache.idb / usr / freeware / apache / include / ap_alloc.h.z / ap_alloc.h
C/C++ Source or Header  |  2001-01-10  |  17KB  |  416 lines

  1. /* ====================================================================
  2.  * Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer. 
  10.  *
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in
  13.  *    the documentation and/or other materials provided with the
  14.  *    distribution.
  15.  *
  16.  * 3. All advertising materials mentioning features or use of this
  17.  *    software must display the following acknowledgment:
  18.  *    "This product includes software developed by the Apache Group
  19.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  20.  *
  21.  * 4. The names "Apache Server" and "Apache Group" must not be used to
  22.  *    endorse or promote products derived from this software without
  23.  *    prior written permission. For written permission, please contact
  24.  *    apache@apache.org.
  25.  *
  26.  * 5. Products derived from this software may not be called "Apache"
  27.  *    nor may "Apache" appear in their names without prior written
  28.  *    permission of the Apache Group.
  29.  *
  30.  * 6. Redistributions of any form whatsoever must retain the following
  31.  *    acknowledgment:
  32.  *    "This product includes software developed by the Apache Group
  33.  *    for use in the Apache HTTP server project (http://www.apache.org/)."
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  36.  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46.  * OF THE POSSIBILITY OF SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Group and was originally based
  51.  * on public domain software written at the National Center for
  52.  * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  53.  * For more information on the Apache Group and the Apache HTTP server
  54.  * project, please see <http://www.apache.org/>.
  55.  *
  56.  */
  57.  
  58. #ifndef APACHE_ALLOC_H
  59. #define APACHE_ALLOC_H
  60.  
  61. #ifdef __cplusplus
  62. extern "C" {
  63. #endif
  64.  
  65. /*
  66.  * Resource allocation routines...
  67.  *
  68.  * designed so that we don't have to keep track of EVERYTHING so that
  69.  * it can be explicitly freed later (a fundamentally unsound strategy ---
  70.  * particularly in the presence of die()).
  71.  *
  72.  * Instead, we maintain pools, and allocate items (both memory and I/O
  73.  * handlers) from the pools --- currently there are two, one for per
  74.  * transaction info, and one for config info.  When a transaction is over,
  75.  * we can delete everything in the per-transaction pool without fear, and
  76.  * without thinking too hard about it either.
  77.  *
  78.  * rst
  79.  */
  80.  
  81. /* Arenas for configuration info and transaction info
  82.  * --- actual layout of the pool structure is private to 
  83.  * alloc.c.  
  84.  */
  85.  
  86.  /* Need declaration of DIR on Win32 */
  87. #ifdef WIN32
  88. #include "../os/win32/readdir.h"
  89. #endif
  90.  
  91. typedef struct pool pool;
  92. typedef struct pool ap_pool;
  93.  
  94. pool * ap_init_alloc(void);        /* Set up everything */
  95. void ap_cleanup_alloc(void);
  96. API_EXPORT(pool *) ap_make_sub_pool(pool *);    /* All pools are subpools of permanent_pool */
  97. #if defined(EAPI)
  98. typedef enum { AP_POOL_RD, AP_POOL_RW } ap_pool_lock_mode;
  99. int ap_shared_pool_possible(void);
  100. void ap_init_alloc_shared(int);
  101. void ap_kill_alloc_shared(void);
  102. API_EXPORT(pool *) ap_make_shared_sub_pool(pool *);
  103. API_EXPORT(int) ap_acquire_pool(pool *, ap_pool_lock_mode);
  104. API_EXPORT(int) ap_release_pool(pool *);
  105. #endif
  106. API_EXPORT(void) ap_destroy_pool(pool *);
  107.  
  108. /* pools have nested lifetimes -- sub_pools are destroyed when the
  109.  * parent pool is cleared.  We allow certain liberties with operations
  110.  * on things such as tables (and on other structures in a more general
  111.  * sense) where we allow the caller to insert values into a table which
  112.  * were not allocated from the table's pool.  The table's data will
  113.  * remain valid as long as all the pools from which its values are
  114.  * allocated remain valid.
  115.  *
  116.  * For example, if B is a sub pool of A, and you build a table T in
  117.  * pool B, then it's safe to insert data allocated in A or B into T
  118.  * (because B lives at most as long as A does, and T is destroyed when
  119.  * B is cleared/destroyed).  On the other hand, if S is a table in
  120.  * pool A, it is safe to insert data allocated in A into S, but it
  121.  * is *not safe* to insert data allocated from B into S... because
  122.  * B can be cleared/destroyed before A is (which would leave dangling
  123.  * pointers in T's data structures).
  124.  *
  125.  * In general we say that it is safe to insert data into a table T
  126.  * if the data is allocated in any ancestor of T's pool.  This is the
  127.  * basis on which the POOL_DEBUG code works -- it tests these ancestor
  128.  * relationships for all data inserted into tables.  POOL_DEBUG also
  129.  * provides tools (ap_find_pool, and ap_pool_is_ancestor) for other
  130.  * folks to implement similar restrictions for their own data
  131.  * structures.
  132.  *
  133.  * However, sometimes this ancestor requirement is inconvenient --
  134.  * sometimes we're forced to create a sub pool (such as through
  135.  * ap_sub_req_lookup_uri), and the sub pool is guaranteed to have
  136.  * the same lifetime as the parent pool.  This is a guarantee implemented
  137.  * by the *caller*, not by the pool code.  That is, the caller guarantees
  138.  * they won't destroy the sub pool individually prior to destroying the
  139.  * parent pool.
  140.  *
  141.  * In this case the caller must call ap_pool_join() to indicate this
  142.  * guarantee to the POOL_DEBUG code.  There are a few examples spread
  143.  * through the standard modules.
  144.  */
  145. #ifndef POOL_DEBUG
  146. #ifdef ap_pool_join
  147. #undef ap_pool_join
  148. #endif
  149. #define ap_pool_join(a,b)
  150. #else
  151. API_EXPORT(void) ap_pool_join(pool *p, pool *sub);
  152. API_EXPORT(pool *) ap_find_pool(const void *ts);
  153. API_EXPORT(int) ap_pool_is_ancestor(pool *a, pool *b);
  154. #endif
  155.  
  156. /* Clearing out EVERYTHING in an pool... destroys any sub-pools */
  157.  
  158. API_EXPORT(void) ap_clear_pool(struct pool *);
  159.  
  160. /* Preparing for exec() --- close files, etc., but *don't* flush I/O
  161.  * buffers, *don't* wait for subprocesses, and *don't* free any memory.
  162.  */
  163.  
  164. API_EXPORT(void) ap_cleanup_for_exec(void);
  165.  
  166. /* routines to allocate memory from an pool... */
  167.  
  168. API_EXPORT(void *) ap_palloc(struct pool *, int nbytes);
  169. API_EXPORT(void *) ap_pcalloc(struct pool *, int nbytes);
  170. API_EXPORT(char *) ap_pstrdup(struct pool *, const char *s);
  171. /* make a nul terminated copy of the n characters starting with s */
  172. API_EXPORT(char *) ap_pstrndup(struct pool *, const char *s, int n);
  173. API_EXPORT_NONSTD(char *) ap_pstrcat(struct pool *,...);    /* all '...' must be char* */
  174. API_EXPORT_NONSTD(char *) ap_psprintf(struct pool *, const char *fmt, ...)
  175.     __attribute__((format(printf,2,3)));
  176. API_EXPORT(char *) ap_pvsprintf(struct pool *, const char *fmt, va_list);
  177.  
  178. /* array and alist management... keeping lists of things.
  179.  * Common enough to want common support code ...
  180.  */
  181.  
  182. typedef struct {
  183.     ap_pool *pool;
  184.     int elt_size;
  185.     int nelts;
  186.     int nalloc;
  187.     char *elts;
  188. } array_header;
  189.  
  190. API_EXPORT(array_header *) ap_make_array(pool *p, int nelts, int elt_size);
  191. API_EXPORT(void *) ap_push_array(array_header *);
  192. API_EXPORT(void) ap_array_cat(array_header *dst, const array_header *src);
  193. API_EXPORT(array_header *) ap_append_arrays(pool *, const array_header *,
  194.                      const array_header *);
  195.  
  196. /* ap_array_pstrcat generates a new string from the pool containing
  197.  * the concatenated sequence of substrings referenced as elements within
  198.  * the array.  The string will be empty if all substrings are empty or null,
  199.  * or if there are no elements in the array.
  200.  * If sep is non-NUL, it will be inserted between elements as a separator.
  201.  */
  202. API_EXPORT(char *) ap_array_pstrcat(pool *p, const array_header *arr,
  203.                                     const char sep);
  204.  
  205. /* copy_array copies the *entire* array.  copy_array_hdr just copies
  206.  * the header, and arranges for the elements to be copied if (and only
  207.  * if) the code subsequently does a push or arraycat.
  208.  */
  209.  
  210. API_EXPORT(array_header *) ap_copy_array(pool *p, const array_header *src);
  211. API_EXPORT(array_header *) ap_copy_array_hdr(pool *p, const array_header *src);
  212.  
  213.  
  214. /* Tables.  Implemented alist style, for now, though we try to keep
  215.  * it so that imposing a hash table structure on top in the future
  216.  * wouldn't be *too* hard...
  217.  *
  218.  * Note that key comparisons for these are case-insensitive, largely
  219.  * because that's what's appropriate and convenient everywhere they're
  220.  * currently being used...
  221.  */
  222.  
  223. typedef struct table table;
  224.  
  225. typedef struct {
  226.     char *key;        /* maybe NULL in future;
  227.              * check when iterating thru table_elts
  228.              */
  229.     char *val;
  230. } table_entry;
  231.  
  232. API_EXPORT(table *) ap_make_table(pool *p, int nelts);
  233. API_EXPORT(table *) ap_copy_table(pool *p, const table *);
  234. API_EXPORT(void) ap_clear_table(table *);
  235. API_EXPORT(const char *) ap_table_get(const table *, const char *);
  236. API_EXPORT(void) ap_table_set(table *, const char *name, const char *val);
  237. API_EXPORT(void) ap_table_setn(table *, const char *name, const char *val);
  238. API_EXPORT(void) ap_table_merge(table *, const char *name, const char *more_val);
  239. API_EXPORT(void) ap_table_mergen(table *, const char *name, const char *more_val);
  240. API_EXPORT(void) ap_table_unset(table *, const char *key);
  241. API_EXPORT(void) ap_table_add(table *, const char *name, const char *val);
  242. API_EXPORT(void) ap_table_addn(table *, const char *name, const char *val);
  243. API_EXPORT(void) ap_table_do(int (*comp) (void *, const char *, const char *), void *rec,
  244.               const table *t,...);
  245.  
  246. API_EXPORT(table *) ap_overlay_tables(pool *p, const table *overlay, const table *base);
  247.  
  248. /* Conceptually, ap_overlap_tables does this:
  249.  
  250.     array_header *barr = ap_table_elts(b);
  251.     table_entry *belt = (table_entry *)barr->elts;
  252.     int i;
  253.  
  254.     for (i = 0; i < barr->nelts; ++i) {
  255.     if (flags & AP_OVERLAP_TABLES_MERGE) {
  256.         ap_table_mergen(a, belt[i].key, belt[i].val);
  257.     }
  258.     else {
  259.         ap_table_setn(a, belt[i].key, belt[i].val);
  260.     }
  261.     }
  262.  
  263.     Except that it is more efficient (less space and cpu-time) especially
  264.     when b has many elements.
  265.  
  266.     Notice the assumptions on the keys and values in b -- they must be
  267.     in an ancestor of a's pool.  In practice b and a are usually from
  268.     the same pool.
  269. */
  270. #define AP_OVERLAP_TABLES_SET    (0)
  271. #define AP_OVERLAP_TABLES_MERGE    (1)
  272. API_EXPORT(void) ap_overlap_tables(table *a, const table *b, unsigned flags);
  273.  
  274. /* XXX: these know about the definition of struct table in alloc.c.  That
  275.  * definition is not here because it is supposed to be private, and by not
  276.  * placing it here we are able to get compile-time diagnostics from modules
  277.  * written which assume that a table is the same as an array_header. -djg
  278.  */
  279. #define ap_table_elts(t) ((array_header *)(t))
  280. #define ap_is_empty_table(t) (((t) == NULL)||(((array_header *)(t))->nelts == 0))
  281.  
  282. /* routines to remember allocation of other sorts of things...
  283.  * generic interface first.  Note that we want to have two separate
  284.  * cleanup functions in the general case, one for exec() preparation,
  285.  * to keep CGI scripts and the like from inheriting access to things
  286.  * they shouldn't be able to touch, and one for actually cleaning up,
  287.  * when the actual server process wants to get rid of the thing,
  288.  * whatever it is.  
  289.  *
  290.  * kill_cleanup disarms a cleanup, presumably because the resource in
  291.  * question has been closed, freed, or whatever, and it's scarce
  292.  * enough to want to reclaim (e.g., descriptors).  It arranges for the
  293.  * resource not to be cleaned up a second time (it might have been
  294.  * reallocated).  run_cleanup does the same, but runs it first.
  295.  *
  296.  * Cleanups are identified for purposes of finding & running them off by the
  297.  * plain_cleanup and data, which should presumably be unique.
  298.  *
  299.  * NB any code which invokes register_cleanup or kill_cleanup directly
  300.  * is a critical section which should be guarded by block_alarms() and
  301.  * unblock_alarms() below...
  302.  */
  303.  
  304. API_EXPORT(void) ap_register_cleanup(pool *p, void *data,
  305.                   void (*plain_cleanup) (void *),
  306.                   void (*child_cleanup) (void *));
  307.  
  308. API_EXPORT(void) ap_kill_cleanup(pool *p, void *data, void (*plain_cleanup) (void *));
  309. API_EXPORT(void) ap_run_cleanup(pool *p, void *data, void (*cleanup) (void *));
  310.  
  311. /* A "do-nothing" cleanup, for register_cleanup; it's faster to do
  312.  * things this way than to test for NULL. */
  313. API_EXPORT_NONSTD(void) ap_null_cleanup(void *data);
  314.  
  315. /* The time between when a resource is actually allocated, and when it
  316.  * its cleanup is registered is a critical section, during which the
  317.  * resource could leak if we got interrupted or timed out.  So, anything
  318.  * which registers cleanups should bracket resource allocation and the
  319.  * cleanup registry with these.  (This is done internally by run_cleanup).
  320.  *
  321.  * NB they are actually implemented in http_main.c, since they are bound
  322.  * up with timeout handling in general...
  323.  */
  324.  
  325. #ifdef TPF
  326. #define ap_block_alarms() (0)
  327. #define ap_unblock_alarms() (0)
  328. #else
  329. API_EXPORT(void) ap_block_alarms(void);
  330. API_EXPORT(void) ap_unblock_alarms(void);
  331. #endif /* TPF */
  332.  
  333. /* Common cases which want utility support..
  334.  * the note_cleanups_for_foo routines are for 
  335.  */
  336.  
  337. API_EXPORT(FILE *) ap_pfopen(struct pool *, const char *name, const char *fmode);
  338. API_EXPORT(FILE *) ap_pfdopen(struct pool *, int fd, const char *fmode);
  339. API_EXPORT(int) ap_popenf(struct pool *, const char *name, int flg, int mode);
  340.  
  341. API_EXPORT(void) ap_note_cleanups_for_file(pool *, FILE *);
  342. API_EXPORT(void) ap_note_cleanups_for_fd(pool *, int);
  343. #ifdef WIN32
  344. API_EXPORT(void) ap_note_cleanups_for_h(pool *, HANDLE);
  345. #endif
  346. API_EXPORT(void) ap_kill_cleanups_for_fd(pool *p, int fd);
  347.  
  348. API_EXPORT(void) ap_note_cleanups_for_socket(pool *, int);
  349. API_EXPORT(void) ap_kill_cleanups_for_socket(pool *p, int sock);
  350. API_EXPORT(int) ap_psocket(pool *p, int, int, int);
  351. API_EXPORT(int) ap_pclosesocket(pool *a, int sock);
  352.  
  353. API_EXPORT(regex_t *) ap_pregcomp(pool *p, const char *pattern, int cflags);
  354. API_EXPORT(void) ap_pregfree(pool *p, regex_t * reg);
  355.  
  356. /* routines to note closes... file descriptors are constrained enough
  357.  * on some systems that we want to support this.
  358.  */
  359.  
  360. API_EXPORT(int) ap_pfclose(struct pool *, FILE *);
  361. API_EXPORT(int) ap_pclosef(struct pool *, int fd);
  362. #ifdef WIN32
  363. API_EXPORT(int) ap_pcloseh(struct pool *, HANDLE hDevice);
  364. #endif
  365.  
  366. /* routines to deal with directories */
  367. API_EXPORT(DIR *) ap_popendir(pool *p, const char *name);
  368. API_EXPORT(void) ap_pclosedir(pool *p, DIR * d);
  369.  
  370. /* ... even child processes (which we may want to wait for,
  371.  * or to kill outright, on unexpected termination).
  372.  *
  373.  * ap_spawn_child is a utility routine which handles an awful lot of
  374.  * the rigamarole associated with spawning a child --- it arranges
  375.  * for pipes to the child's stdin and stdout, if desired (if not,
  376.  * set the associated args to NULL).  It takes as args a function
  377.  * to call in the child, and an argument to be passed to the function.
  378.  */
  379.  
  380. enum kill_conditions {
  381.     kill_never,            /* process is never sent any signals */
  382.     kill_always,        /* process is sent SIGKILL on pool cleanup */
  383.     kill_after_timeout,        /* SIGTERM, wait 3 seconds, SIGKILL */
  384.     just_wait,            /* wait forever for the process to complete */
  385.     kill_only_once        /* send SIGTERM and then wait */
  386. };
  387.  
  388. typedef struct child_info child_info;
  389. API_EXPORT(void) ap_note_subprocess(pool *a, pid_t pid,
  390.                     enum kill_conditions how);
  391. API_EXPORT(int) ap_spawn_child(pool *, int (*)(void *, child_info *),
  392.                    void *, enum kill_conditions,
  393.                    FILE **pipe_in, FILE **pipe_out,
  394.                    FILE **pipe_err);
  395.  
  396. /* magic numbers --- min free bytes to consider a free pool block useable,
  397.  * and the min amount to allocate if we have to go to malloc() */
  398.  
  399. #ifndef BLOCK_MINFREE
  400. #define BLOCK_MINFREE 4096
  401. #endif
  402. #ifndef BLOCK_MINALLOC
  403. #define BLOCK_MINALLOC 8192
  404. #endif
  405.  
  406. /* Finally, some accounting */
  407.  
  408. API_EXPORT(long) ap_bytes_in_pool(pool *p);
  409. API_EXPORT(long) ap_bytes_in_free_blocks(void);
  410.  
  411. #ifdef __cplusplus
  412. }
  413. #endif
  414.  
  415. #endif    /* !APACHE_ALLOC_H */
  416.